package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Dearsil
 * @Date: 2024/11/10 15:10
 * @Version: 1.0
 * @Description: Hello Description!
 */
@Service
public class WechatServiceImpl implements WechatService {

    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appId}")
    private String appid;

    @Value("${wechat.appSecret}")
    private String secret;


    /**
     * 获取openid
     *
     * @param code 小程序端传递的code
     * @return openid
     */
    @Override
    public String getOpenid(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid",appid);
        paramMap.put("secret",secret);
        paramMap.put("js_code",code);

        String result = HttpUtil.get(REQUEST_URL, paramMap);
        // 将结果字符串转换成Json格式
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 判断请求是否成功
        if(!ObjectUtils.isEmpty(jsonObject.getInt("errcode"))){
            throw new BaseException("获取openid失败",jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("openid");
    }

    /**
     * 获取手机号
     *
     * @param phoneCode 小程序端传递的detailCode
     * @return 手机号
     */
    @Override
    public String getPhone(String phoneCode) {
        String accessToken = getAccessToken();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code",phoneCode);

        String result = HttpUtil.post(PHONE_REQUEST_URL + accessToken, JSONUtil.toJsonStr(paramMap));
        // 将结果字符串转换成Json格式
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 判断请求是否成功
        if(jsonObject.getInt("errcode") != 0){
            throw new BaseException("获取手机号失败",jsonObject.getStr("errmsg"));
        }
        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    private String getAccessToken() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid",appid);
        paramMap.put("secret",secret);

        String result = HttpUtil.get(TOKEN_URL, paramMap);
        // 将结果字符串转换成Json格式
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 判断请求是否成功
        if(ObjectUtils.isNotEmpty(jsonObject.getInt("errcode"))){
            throw new BaseException("获取accessToken失败",jsonObject.getStr("errmsg"));
        }

        return jsonObject.getStr("access_token");
    }
}
